import 'package:flutter/material.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:intl/intl.dart';
import 'package:badges/badges.dart' as badges;

class AppBottomAppBar extends StatelessWidget {
  List<BottomBarItem> items;
  BottomItemCallback itemClickCallback;
  int selectedIndex;
  Color? backgroundColor;
  bool showElevation;
  double containerHeight;
  bool showFloatFabLocation;

  AppBottomAppBar({
    required this.items,
    required this.selectedIndex,
    required this.itemClickCallback,
    this.showElevation = false,
    this.backgroundColor,
    this.containerHeight = 60,
    this.showFloatFabLocation = true,
    Key? key,
  }) : super(key: key) {
    if (showFloatFabLocation) {
      /*items.insert(
          2,
          BottomBarItem(
            title: const Text("主页"),
            mIcon: const Icon(Icons.home_filled),
            activeColor: Colors.blue,
            skip: true,
          ));*/
    }
  }

  @override
  Widget build(BuildContext context) {
    Color bgColor = backgroundColor ?? Theme.of(context).bottomAppBarColor;
    //double width = MediaQuery.of(context).size.width;
    //double itemWidth = width / items.length;
    return SafeArea(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        decoration: BoxDecoration(
          color: bgColor,
          boxShadow: [
            if (showElevation)
              const BoxShadow(
                color: Colors.black12,
                blurRadius: 2,
              ),
          ],
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.r),
            topRight: Radius.circular(10.r),
          ),
        ),
        width: double.infinity,
        height: containerHeight.h,
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: items.map((e) {
            var index = items.indexOf(e);
            if (e.skip) {
              return Container();
            }
            return GestureDetector(
              onTap: () {
                itemClickCallback.call(index);
              },
              child: Container(
                child: BottomBarItemWidget(
                  bottomBarItem: e,
                  isSelect: index == selectedIndex,
                ),
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

class BottomBarItemWidget extends StatelessWidget {
  final BottomBarItem bottomBarItem;
  final bool isSelect;

  const BottomBarItemWidget({
    required this.bottomBarItem,
    required this.isSelect,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Semantics(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            badges.Badge(
              badgeContent: Text(
                "${bottomBarItem.badge}",
                style: TextStyle(color: Colors.white, fontSize: 12.sp),
              ),
              position: badges.BadgePosition.topEnd(),
              showBadge: bottomBarItem.badge != 0,
              child: IconTheme(
                data: IconThemeData(
                    size: 24,
                    color: isSelect ? bottomBarItem.activeColor : null),
                child: bottomBarItem.mIcon,
              ),
            ),
            DefaultTextStyle.merge(
              style: TextStyle(
                color: isSelect ? bottomBarItem.activeColor : null,
              ),
              maxLines: 1,
              child: bottomBarItem.title,
            )
          ],
        ),
      ),
    );
  }
}

class AlmostEndFloatFabLocation extends FloatingActionButtonLocation {
  FloatingActionButtonLocation? location;
  late double offsetX; // X方向的偏移量
  late double offsetY; // Y方向的偏移量

  AlmostEndFloatFabLocation(this.location,
      {this.offsetX = 0, this.offsetY = 0});

  @override
  Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
    Offset offset = location!.getOffset(scaffoldGeometry);
    return Offset(offset.dx + offsetX, offset.dy + offsetY);
  }
}

class NoScalingAnimation extends FloatingActionButtonAnimator {
  double _x = 0;
  double _y = 0;

  @override
  Offset getOffset(
      {required Offset begin, required Offset end, required double progress}) {
    _x = begin.dx + (end.dx - begin.dx) * progress;
    _y = begin.dy + (end.dy - begin.dy) * progress;
    return Offset(_x, _y);
  }

  @override
  Animation<double> getRotationAnimation({required Animation<double> parent}) {
    return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
  }

  @override
  Animation<double> getScaleAnimation({required Animation<double> parent}) {
    return Tween<double>(begin: 1.0, end: 1.0).animate(parent);
  }
}

class BottomBarItem {
  final Widget mIcon;
  final Widget title;
  final Color activeColor;
  final int badge;
  final bool skip;
  final int index;

  BottomBarItem({
    required this.mIcon,
    required this.title,
    required this.activeColor,
    this.badge = 0,
    this.skip = false,
    this.index = 0,
  });
}

typedef BottomItemCallback = void Function(int position);
